package com.imooc.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

import java.util.UUID;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;

/**
 * 自定义Post过滤器
 * 请求到了目标服务，返回结果之后，对结果进行加工（返回结果的header设置内容）
 * @author steve
 * @date 2020/6/24 14:23
 */
@Component
public class AddResponseHeaderFilter extends ZuulFilter {


    @Override
    public String filterType() {
        return POST_TYPE;
    }

    /**
     * 过滤的优先级
     * @return
     */
    @Override
    public int filterOrder() {
        return SEND_RESPONSE_FILTER_ORDER -1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 具体实现
     * @return
     */
    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletResponse response = requestContext.getResponse();
        response.setHeader("X-Food", UUID.randomUUID().toString());
        return null;
    }
}
